/**
 * margin and padding 规则
 */
import { UNIT_STR, DIRECTION_MAP, NONNEGATIVE_NUMBER_REGEX_STR } from '../constant';

const getConfig = (type: any, direction: any) => {
    let name = ''
    if (type === 'm' || type === 'margin') {
        name += 'margin'
    }
    // 单项加 60 order m-l-10 最大为50 + 60 padding 从 120 起
    if (type === 'p' || type === 'padding') {
        name += 'padding'
    }
    if (direction) {
        name += `-${direction}`
    }
    return { name }
}

export default {
    regExp: new RegExp(`^(?<type>m|margin|p|padding)(-|)((?<direction>[trblxy])-)?((?<auto>auto)|(?<isMinus>m-)?(?<num>${NONNEGATIVE_NUMBER_REGEX_STR})(?<unit>${UNIT_STR})?)$`),
    render({ groups }) {
        let { type, direction, isMinus, num, unit, auto } = groups
        if (auto) {
            unit = ''
            num = 'auto'
        }
        // if has auto never has isMinus
        if (isMinus) {
            num = 0 - num
        }
        const baseConfig = getConfig(type, direction)
        if (type === 'm') {
            type = 'margin'
        }
        if (type === 'p') {
            type = 'padding'
        }
        return {
            ...baseConfig,
            num,
            css: DIRECTION_MAP
                .get(direction)
                .reduce((t, c) =>
                    [...t, c ? `${type}-${c}: ${num}${unit}` : `${type}: ${num}${unit}`], [])
        }
    }
}